<template>
  <border
    class="annual-rental-trend"
    :moduleType="borderConfig.moduleType"
    :icon="borderConfig.icon"
    :titleText="borderConfig.titleText"
    :goPageUrl="borderConfig.goPageUrl">
    <div class="module-content" slot="module-content">
      <bar-line ref="barLine"
                :title="barLineConfig.title"
                :axis="barLineConfig.axis"
                :xAxis="barLineConfig.xAxis"
                :yAxis="barLineConfig.yAxis"
                :dataZoom="barLineConfig.dataZoom"
                :grid="barLineConfig.grid"
                :config="barLineConfig.config"
                :tooltipsConfig="barLineConfig.tooltipsConfig"
                :seriesData="barLineConfig.seriesData"></bar-line>
      <div class="chart-legend">
        <div class="legend" v-for="(item, index) in barLineName" :key="index">
          <div class="legend-li" v-if="index < limitLength - 1">
            <i :style="{'background': barColor[index === 0 ? 1 : 0]}"></i>
            <span>{{ item.length > 4 ? item.substring(0, 4) : item }}</span>
          </div>
          <div class="legend-li" v-if="index == limitLength - 1">
            <i :style="{'background': barColor[index === 0 ? 1 : 0]}"></i>
            <span>{{ barLineName === 5 ?item.length > 4 ? item.substring(0, 4) : item : '其他' }}</span>
          </div>
        </div>
      </div>
    </div>
  </border>
</template>

<script>
import Border from './../components/Border.vue'
import BarLine from './../components/BarLine.vue'
export default {
  name: 'annualRentalTrend',
  components: {
    Border,
    BarLine
  },
  data () {
    return {
      borderConfig: {
        moduleType: '1x1',
        icon: '',
        titleText: '年度出租趋势',
        goPageUrl: ''
      },
      barLineConfig: {
        title: {
          text: ''
        },
        axis: {
          xAxisNameLength: 5,
          xAxisNameLengthSymbol: ''
        },
        dataZoom: {
          show: false,
          right: 10,
          endValue: 11
        },
        grid: {
          top: 50,
          bottom: 10
        },
        xAxis: {
          axisLabel: {
            interval: 0
          },
          data: []
        },
        yAxis: [
          {
            splitNumber: 4,
            name: '平均租金单价',
            nameTextStyle: {
              color: '#FFFFFF',
              align: 'left',
              padding: [0, 0, 0, 40]
            }
          },
          {
            splitNumber: 4,
            name: '出租率',
            nameTextStyle: {
              color: '#FFFFFF',
              align: 'right',
              padding: [0, 40, 0, 0]
            }
          }
        ],
        tooltipsConfig: {
          tipsColor: ['#2e92cb', '#b60d81'],
          symbol: ['', '']
        },
        seriesData: []
      },
      barLineName: ['出租率', '租金'],
      limitLength: 5,
      barColor: ['#2e92cb', '#b60d81'], // 数据
      refreshInterval: 0,
      apiData: {}
    }
  },
  computed: {
    organId () {
      return this.$store.state.organId
    },
    isIndex () {
      return this.$store.state.isIndex
    }
  },
  methods: {
    showTips  (data) {
      this.$parent.showTips(data)
    },
    // 获取数据
    getData (type, refreshInterval, isRefresh) {
      if (type === 'init') {
        this.operationApi.businessLeaseFeeByMonth({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            this.apiData = res.data.data
            if (!isRefresh) {
              this.handleData()
            }
          } else {
            this.apiData = {}
            this.octagonMessage = [0, 0]
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
        }, res => {
          this.apiData = {}
          this.octagonMessage = [0, 0]
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        if (refreshInterval) {
          this.refreshInterval = refreshInterval
          this.automaticRedrawing()
        }
      } else {
        this.handleData()
      }
    },
    handleData () {
      let data = this.apiData
      let dataZoomLength = window.screen.width > 1890 ? 12 : 12
      let seriesData = []
      let seriesData1 = []
      let xData = []
      if (JSON.stringify(data) !== '{}') {
        data.monthM2Fee.forEach((item, index) => {
          xData.push(item.name.substring(4, item.name.length))
          seriesData.push(Number(item.value / 100))
          seriesData1.push(Number(data.rentalRate[index].value))
        })
      }
      if (xData.length > dataZoomLength) {
        this.barLineConfig.dataZoom.show = true
      } else {
        this.barLineConfig.dataZoom.show = false
      }
      this.barLineConfig.xAxis.data = xData
      this.barLineConfig.seriesData = [
        {
          data: seriesData,
          name: '租金',
          colorIndex: 0,
          yAxisIndex: 0,
          isBar: true
        },
        {
          data: seriesData1,
          name: '出租率',
          colorIndex: 1,
          yAxisIndex: 1,
          isBar: false
        }
      ]
    },
    // 根据接口返回的时间间隔请求接口
    automaticRedrawing () {
      clearInterval(window.annualRentalTrendTimer)
      if (this.refreshInterval > 0 && this.isIndex) {
        window.annualRentalTrendTimer = setInterval(() => {
          this.getData('init', '', true)
        }, this.refreshInterval * 1000)
      }
    }
  },
  mounted () {
  },
  destroyed () {
    clearInterval(window.annualRentalTrendTimer)
  }
}
</script>
<style lang="scss" scoped>
.annual-rental-trend {
  .module-content {
    width: 100%;
    height: 100%;
    .bar-line {
      height: 92%;
    }
    .chart-legend {
      overflow: hidden;
    }
    .legend {
      display: inline-block;
      margin: 0 25px 0 0;
      font-size: 14px;
      height: 22px;
      line-height: 22px;
      color: #A4B3C7;
    }
    .legend-li {
      height: 100%;
      i {
        display: block;
        float: left;
        width: 9px;
        height: 9px;
        vertical-align: top;
        margin: 7px 5px 0 0;
      }
      span {
        display: inline-block;
      }
    }
  }
}
@media screen and (min-width: 1890px) {
  .annual-rental-trend {
    .module-content {
    }
  }
}
</style>
